from selenium.webdriver.common.by import By
from page.base_page import BasePage
from time import sleep


class DashboardPage(BasePage):
    # 仪表盘页面基础路径
    url = "/index.php"

    # 定位器
    dashboard_link_locator = (By.CSS_SELECTOR, "body > div.container > div.page-header > ul > li:nth-child(2) > a")
    notepad_locator = (By.ID, "notepad")
    add_component_btn_locator = (By.XPATH, "/html/body/div[5]/div[2]/p/span/a")  # 添加组件按钮
    component_title_input_locator = (By.ID, "title")  # 组件标题输入框
    save_component_btn_locator = (By.XPATH, "/html/body/div[7]/div[2]/form/div[4]/div/input[1]")  # 保存按钮
    calendar_locator = (By.XPATH, "/html/body/div[5]/div[4]/div/div[1]/div[1]/div[20]")  # 日历特定日期
    switch_component_locator = (By.CSS_SELECTOR, "#widgets > div > div:nth-child(6) > div > div.dash-title > a")

    def click_dashboard(self):
        """点击仪表盘链接"""
        self.find_element(self.dashboard_link_locator).click()
        sleep(2)

    def input_notepad(self, text):
        """在便签中输入内容"""
        self.find_element(self.notepad_locator).send_keys(text)

    def get_notepad_content(self):
        """获取便签中的内容"""
        return self.find_element(self.notepad_locator).get_attribute("value")

    def click_add_component(self):
        """点击“添加组件”按钮"""
        self.find_element(self.add_component_btn_locator).click()
        sleep(2)

    def input_component_title(self, title):
        """输入组件标题（如“销售漏斗”）"""
        self.find_element(self.component_title_input_locator).send_keys(title)
        sleep(1)

    def click_save_component(self):
        """点击“保存”按钮"""
        self.find_element(self.save_component_btn_locator).click()
        sleep(3)

    def add_dashboard_component(self, title):
        """封装完整添加组件流程"""
        self.click_add_component()
        self.input_component_title(title)
        self.click_save_component()

    def click_calendar_date(self):
        """点击日历中的特定日期"""
        self.find_element(self.calendar_locator).click()
        sleep(2)

    def click_switch_component(self):
        """点击仪表盘组件切换按钮"""
        self.find_element(self.switch_component_locator).click()
        sleep(2)

    def switch_dashboard_component(self):
        """完整流程：点击仪表盘链接→点击组件切换按钮"""
        self.click_dashboard()
        self.click_switch_component()